home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snpd0492.zip / DECLS.TXT < prev    next >
Text File  |  1992-04-13  |  3KB  |  73 lines

  1.      ---------------------------------------------
  2.      |  Understanding Complicated C Declarations |
  3.      ---------------------------------------------
  4.  
  5.     This file shows you how to decipher those complicated
  6.     declarations
  7.  
  8.     First, A valid declaration pairs its post symbols with
  9.     pre symbols, using parentheses when necessary. You can
  10.     have more pre than post, but, not more post than pre
  11.     symbols. That is, you must have at least one pre symbol
  12.     for each post symbol for the declaration to be valid.
  13.  
  14.     TABLE:
  15.  
  16.     Symbol    Text                    Order      Direction
  17.     -------------------------------------------------------
  18.     []        array of                1          Post
  19.     ()        function returning      1          Post
  20.     *         pointer to              2          Pre
  21.     <type>    type <type>             3          Pre
  22.     -------------------------------------------------------
  23.  
  24.     OK, So what can we do with this? Lets try one!
  25.     how about:
  26.  
  27.             char *foo[3];
  28.  
  29.     Starting at the name "foo is" we see that the [] symbol
  30.     has the highest order. so we insert the text. "foo is
  31.     an array of 3 elements which are". And then the * comes
  32.     next followed by the type. "foo is an array of 3
  33.     elements which are pointers to char. how about:
  34.  
  35.             char (*foo)[3];
  36.  
  37.     This says "foo is a pointer to an array of 3 chars".
  38.     Don't forget that the parentheses forced the
  39.     precedence to the * symbol (this is not the same
  40.     symbol as () refering to "function returning").
  41.     How about:
  42.  
  43.             int *(*(*foo[4])())[5];
  44.  
  45.     Looks hard? Well following the table start at the name
  46.     foo. The table states that the array symbol has
  47.     precedence so we say "foo is an array of". Then the
  48.     parentheses force the order to "foo is an array of
  49.     pointers to". Then the () is next. "foo is an array of
  50.     pointers to functions returning". Then the parentheses
  51.     force the order to "foo is an array of pointers to
  52.     functions returning pointers to an array of pointers to
  53.     int".
  54.  
  55.     OK, Now lets do it the other way. Suppose we want "foo"
  56.     to be "an array of 5 pointers to functions returning
  57.     pointers to char"
  58.  
  59.       char  *(*foo[5])();
  60.  
  61.     You can get as complex as you want now that you know
  62.     the rules. How about "foo is an array of 3 pointers to
  63.     functions returning pointers to an array of 3 pointers
  64.     to functions returning pointers to int"?
  65.  
  66.       int   *(*(*(*foo[3])())[3])();
  67.  
  68.     If you are like me you could not have understood this
  69.     before you knew the rules that I have pointed out here.
  70.     Good luck and will be "C"ing you.
  71.  
  72.                      THE END.
  73.